package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzInfoService;
import com.augurit.gzcgwsp.qyzz.web.form.YwRqQyzzInfoForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IQyzzService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 解析企业资质xml数据
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class QyzzServiceImpl implements IQyzzService {
	Logger logger = (Logger) LoggerFactory.getLogger(QyzzServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwRqQyzzInfoService ywRqQyzzInfoService;
	
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String qyzzfilepath = localResource.getString("qyzzfilepath");

	// 附件存放路径
	private final String qyzzfujianpath = localResource.getString("qyzzfujianpath");

	// 解析成功反馈xml存放路径
	private final String qyzzfeedbackPath = localResource.getString("qyzzfeedbackPath");

	// 解析异常xml存放路径
	private final String qyzzexceptionPath = localResource.getString("qyzzexceptionPath");

	// 解析后，xml备份
	private final String qyzzbackPath = localResource.getString("qyzzbackPath");

	private Map<String, String> qyzzKeyMap;// xml内容的key

	private Map<String, String> qyzzKeyMapHead;// xml头部的key

	private Map<String, Long> qyzzfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initQyzzMap() {
		// 初始化数据部分对应的map
		qyzzKeyMap = new HashMap<String, String>();
		
		qyzzKeyMap.put("YSL_NO", "YSL_NO");
		qyzzKeyMap.put("approvalNo", "approvalNo");
		qyzzKeyMap.put("businessCode", "businessCode");
		qyzzKeyMap.put("sendTime", "sendTime");
		
		qyzzKeyMap.put("SQDW", "SQDW");
		qyzzKeyMap.put("IEI_ADDRESS", "IEI_ADDRESS");
		qyzzKeyMap.put("IEI_EMAIL", "IEI_EMAIL");
		qyzzKeyMap.put("IEI_POST", "IEI_POST");
		qyzzKeyMap.put("IEI_LINKMAN", "IEI_LINKMAN");
		qyzzKeyMap.put("IEI_TEL", "IEI_TEL");
		qyzzKeyMap.put("IEI_REGMONEY", "IEI_REGMONEY");
		qyzzKeyMap.put("IEI_AREA", "IEI_AREA");
		qyzzKeyMap.put("IEI_HOST", "IEI_HOST");
//		qyzzKeyMap.put("IEI_HOST_ZW", "IEI_HOST_ZW");
		qyzzKeyMap.put("IEI_LICENSENUM", "IEI_LICENSENUM");
		qyzzKeyMap.put("IEI_INTEGERNUM", "IEI_INTEGERNUM");
		qyzzKeyMap.put("IEI_LICENCE", "IEI_LICENCE");
		qyzzKeyMap.put("IEI_KIND", "IEI_KIND");
		qyzzKeyMap.put("IEI_TECHPEOP", "IEI_TECHPEOP");
		qyzzKeyMap.put("IEI_INSTALLPEOP", "IEI_INSTALLPEOP");
		qyzzKeyMap.put("IEI_TOTALPEOP", "IEI_TOTALPEOP");
		qyzzKeyMap.put("IEI_JYNUM", "IEI_JYNUM");
		qyzzKeyMap.put("IEI_GFNUM", "IEI_GFNUM");
		qyzzKeyMap.put("IEI_ENTINST", "IEI_ENTINST");
		//qyzzKeyMap.put("IEI_REMARKS", "IEI_REMARKS");

		

		// 初始化头部对应的map
		qyzzKeyMapHead = new HashMap<String, String>();
		qyzzKeyMapHead.put("sn", "sn");
		qyzzKeyMapHead.put("sender", "sender");
		// qyzzKeyMapHead.put("receiver", "receiver");
		qyzzKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		qyzzfujianMap = new HashMap<String, Long>();
		qyzzfujianMap.put("FJ_01", 5l);
		qyzzfujianMap.put("FJ_02", 6l);
		qyzzfujianMap.put("FJ_03", 7l);
		qyzzfujianMap.put("FJ_04", 8l);
		qyzzfujianMap.put("FJ_05", 9l);
		qyzzfujianMap.put("FJ_06", 10l);
		qyzzfujianMap.put("FJ_07", 11l);
		qyzzfujianMap.put("FJ_08", 12l);
		//qyzzfujianMap.put("FJ_09", 13l);
		qyzzfujianMap.put("FJ_09", 14l);
		qyzzfujianMap.put("FJ_10", 15l);
		qyzzfujianMap.put("FJ_11", 16l);
		qyzzfujianMap.put("FJ_12", 17l);
		qyzzfujianMap.put("FJ_13", 18l);
		qyzzfujianMap.put("FJ_14", 19l);
		qyzzfujianMap.put("FJ_15", 20l);
		qyzzfujianMap.put("FJ_16", 21l);
		qyzzfujianMap.put("FJ_17", 22l);
		qyzzfujianMap.put("FJ_18", 23l);
		qyzzfujianMap.put("FJ_19", 24l);
	}

	public void readXml(String projectPath) {
		if (qyzzKeyMap == null)
			this.initQyzzMap();

		File file = new File(qyzzfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(qyzzKeyMap, filePath,qyzzfeedbackPath, qyzzexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(qyzzKeyMapHead,files[i], qyzzfeedbackPath, qyzzexceptionPath);

						// 把数据部分保存到数据库
						YwRqQyzzInfoForm form = new YwRqQyzzInfoForm();
						for (String mapKey : qyzzKeyMap.keySet()) {
							// TODO 把值放到form中
							form.setSqdw(parseXMLService.getMapValue(qyzzKeyMap.get("SQDW")));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(qyzzKeyMap.get("SQDW")),"rqrsqjazwxqyzz");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setYslNo(parseXMLService.getMapValue(qyzzKeyMap.get("YSL_NO")));
							form.setAddress(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_ADDRESS")));
							form.setEmail(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_EMAIL")));
							form.setPost(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_POST")));
							form.setYwlxr(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_LINKMAN")));
							form.setYwlxrtel(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_TEL")));
							form.setZczj(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_REGMONEY")));
							form.setCsmj(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_AREA")));
							form.setFddbr(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_HOST")));
							form.setZsbh(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_LICENSENUM")));
							form.setZzdj(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_INTEGERNUM")));
							form.setGszcdjh(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_LICENCE")));
							form.setJjlx(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_KIND")));
							form.setRsJsry(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_TECHPEOP")));
							form.setRsWxg(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_INSTALLPEOP")));							
							form.setRsTotal(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_TOTALPEOP")));
							form.setDwslJyrq(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_JYNUM")));
							form.setDwslGyrq(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_GFNUM")));
							form.setQyRemarks(parseXMLService.getMapValue(qyzzKeyMap.get("IEI_ENTINST")));
							form.setSqrq(new Date());							
							form.setApprovalNo(parseXMLService.getMapValue(qyzzKeyMap.get("approvalNo")));							
							form.setBusinessCode(parseXMLService.getMapValue(qyzzKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(qyzzKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setSarq(new Date());
							form.setSendNotice("Y");
							form.setBranch("rqrsqjazwxqyzz");
							form.setYsl(1l);
						}
						
						// 调用DAO，保存到数据库
						try {
							ywRqQyzzInfoService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									qyzzKeyMapHead, qyzzfeedbackPath, qyzzexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, qyzzfujianpath, qyzzfeedbackPath, qyzzexceptionPath, qyzzKeyMapHead,
								files, qyzzfujianMap, i, form.getQyid(), form.getId(), "rqrsqjazwxqyzz");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								qyzzKeyMapHead, qyzzfeedbackPath, qyzzexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, qyzzbackPath + fileName,fileName);
				}
			}
		}
	}

}
